﻿using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using Core.DAL;
using Core.Utils;

namespace Core.BOL
{
    public class StaffRepository
    {
        private ScoutNetDataContext context = null;

        #region Construtor
        public StaffRepository()
        {
            context = GeneralUtils.GetDataContext();
        }
        #endregion

        #region Methodes
        public tSite_Staff GetStaffMemberByID(int ID)
        {
            var query = (from st in context.tSite_Staffs
                         join s in context.tSite_Sections on st.SID equals s.ID
                         where st.ID == ID
                         select st).SingleOrDefault();
            return query; 
        }

        public void InsertStaff(tSite_Staff staff)
        {
            context.tSite_Staffs.InsertOnSubmit(staff);
        }

        public void DeleteStaffMember(int id)
        {
            tSite_Staff tStaff = context.tSite_Staffs.SingleOrDefault(p => p.ID == id);
            if (tStaff != null)
            {
                if (tStaff.Image != null)
                {
                    Imaging.DeleteImageCached(tStaff.Image);
                    File.Delete(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["StaffPath"] + tStaff.Image.ToString() + ".jpg"));
                }
                context.tSite_Staffs.DeleteOnSubmit(tStaff);
            }
        }

        public List<tSite_Staff> GetAllModeratorMembers()
        {
            var query = (from st in context.tSite_Staffs
                         join s in context.tSite_Sections on st.SID equals s.ID
                         where st.IsModerator == true && st.DateOut == null
                         orderby st.IsModerator
                         select st).ToList();
            return query;
        }

        public List<tSite_Staff> GetStaffBySystemName(string sectionName)
        {
            var query = (from st in context.tSite_Staffs
                         join s in context.tSite_Sections on st.SID equals s.ID
                         where s.SystemName == sectionName && st.DateOut == null
                         orderby st.IsModerator descending, st.DateIn ascending
                         select st).ToList();
            return query;
        }

        public List<tSite_Staff> GetStaffBySystemNameAndYear(string sectionName, int archiveYear)
        {
            var query = (from st in context.tSite_Staffs
                         join s in context.tSite_Sections on st.SID equals s.ID
                         where s.SystemName == sectionName && st.DateIn <= archiveYear && st.DateOut >= archiveYear && st.DateOut != null
                         orderby st.IsModerator descending, st.DateIn ascending
                         select st).ToList();
            return query;
        }

        public int GetLastYearBySystemName(string systemName)
        {
            var query = (from st in context.tSite_Staffs
                         join s in context.tSite_Sections on st.SID equals s.ID
                         where s.SystemName == systemName && st.DateOut != null
                         select st.DateOut);

            return (query.Count() > 0) ? (int)query.Max() : -1;
        }

        public int GetFirstYearBySystemName(string systemName)
        {
            var query = (from st in context.tSite_Staffs
                         join s in context.tSite_Sections on st.SID equals s.ID
                         where s.SystemName == systemName && st.DateOut != null
                         select st.DateIn);

            return (query.Count() > 0) ? query.Min() : -1;
        }

        public void Save()
        {
            context.SubmitChanges();
        }
        #endregion
    }
}
